define(['app'], function(app) {
    app.controller('EditMockProxyController', function($rootScope, $scope, $http, $uibModalInstance, xDialog,
            xContext, toastr) {
        var self = this;
        self.proxyPort = '';
        self.proxyRoutes = [];
        self.proxyPortRange = [0, 65535];

        var modified = null;

        self.init = function() {
            loadProxyConfig();
        }

        self.refresh = function() {
            loadProxyConfig(true);
        }

        function loadProxyConfig(refresh) {
            $http.post('/mock/proxy/load', { module: xContext.module }).then(function(response) {
                if (response.data == 'required') {
                    xDialog.alert('提示信息', '数据模块【' + xContext.module + '】不存在！');
                } else if (response.status == 200) {
                    modified = response.data.modified;
                    self.proxyPort = response.data.port;
                    var routes = _.map(response.data.routes, function(route) {
                        return {
                            name: route.from,
                            value: route.to,
                            checked: route.enabled,
                            disabled: $rootScope.sessionUser.role != 'admin'
                        };
                    });
                    if ($rootScope.sessionUser.role == 'admin') {
                        routes.push({
                            name: '',
                            value: '',
                            checked: true
                        })
                    }
                    self.proxyRoutes = routes;
                    self.proxyPortRange = response.data.ranges;
                    if (refresh) {
                        toastr.success('配置已刷新！');
                    }
                }
            });
        }

        self.save = function(valid) {
            if (valid) {
                xDialog.confirm('确认信息', '您确定要保存配置吗？').then(function(yes) {
                    if (yes) {
                        var routes = _.filter(self.proxyRoutes, function(route) {
                            return route.name != '';
                        });
                        routes = _.map(routes, function(route) {
                            return {
                                from: route.name,
                                to: route.value,
                                enabled: route.checked
                            };
                        });
                        $http.put('/mock/proxy/save', {
                            modified: modified,
                            module: xContext.module,
                            port: self.proxyPort,
                            routes: routes
                        }).then(function(response) {
                            if (response.data == 'required') {
                                xDialog.alert('提示信息', '数据模块【' + xContext.module + '】不存在！');
                            } else if (response.data == 'expired') {
                                xDialog.alert('提示信息', '配置已被更新，请刷新后再保存！');
                            } else if (response.data == 'used') {
                                xDialog.alert('提示信息', '代理端口【' + self.proxyPort + '】已被占用！');
                            } else if (response.data == 'offset') {
                                xDialog.alert('提示信息', '代理端口【' + self.proxyPort + '】范围不正确！');
                            } else if (response.data) {
                                toastr.success('配置已保存！');
                                $uibModalInstance.close(true);
                            }
                        });
                    }
                });
            }
        }

        self.close = function() {
            $uibModalInstance.close();
        }
    });
});
